// 删除 polyline 关联的label
export const removeShapeLengthLabels = (shape, state) => {
    let item = state.polylineLengthLabel.find((i) => i.shapeId === shape.id);
    if (item) {
        item.labels.map((label) => {
            state.graphicLayer.removeGraphic(label, true);
        })
        item.labels.length = 0;
    }
}
// 添加 polyline 两点的距离label
export const polylineLength = (shape, state) => {
    removeShapeLengthLabels(shape, state);
    let shapeInfo = shape.toJSON();
    let positions = shapeInfo.positions;
    let lineSplit = [];
    for (let i = 0; i < positions.length - 1; i++) {
        lineSplit.push([
            positions[i],
            positions[i + 1],
        ]);
    }
    let labels = [];
    // 计算各线段长度
    lineSplit.map(([startCartesian, endCartesian]) => {
        const distance = new mars3d.graphic.DistanceMeasure({
            positions: [startCartesian, endCartesian],
        })
        const strDis = mars3d.MeasureUtil.formatDistance(distance.distance);
        const labelElement = new mars3d.graphic.LabelEntity({
            position: endCartesian,
            style: {
                text: strDis,
                font_size: 46, // 字号放大一倍
                scale: 0.5, // scale传0.5
                font_family: "楷体",
                color: "#00ffff",
                outline: true,
                outlineColor: "#000000",
                outlineWidth: 2,
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                visibleDepth: false
            },
            attr: {remark: "扩展参数", shapeId: shape.id}
        })
        state.graphicLayer.addGraphic(labelElement);
        labels.push(labelElement);
    });
    let findShape = state.polylineLengthLabel.find((i) => i.shapeId === shape.id);
    if (findShape) {
        findShape.labels = labels;
    } else {
        state.polylineLengthLabel.push({
            shapeId: shape.id,
            labels: labels
        })
    }
}

// 设置图形的 面积，长度距离
export const getShapeArea_LineLength = (shape, state, props) => {
    if (shape.options.type === "polyline") {
        if (props.shapeConfig.polylineLength) {
            polylineLength(shape, state);
            const strDis = mars3d.MeasureUtil.formatDistance(shape.distance)
            shape.setStyle({
                label: {
                    text: strDis
                }
            })
        }
        return;
    }
    if (!props.shapeConfig.shapeArea) {
        return false;
    }
    const shapeArea = mars3d.MeasureUtil.formatArea(shape.area)
    shape.setStyle({
        label: {
            text: shapeArea
        }
    })
}